﻿using System;
using System.Collections.Generic;
using System.Text;
using com.lover.astd.common.logic;
using com.lover.astd.common.model;

namespace com.lover.astd.common.logicexe.equip
{
    public class StoreExe : ExeBase
    {
        public StoreExe() { _name = ConfigStrings.S_Store; _readable = ConfigStrings.SR_Store; }
        public override long execute()
        {
            int merge_gem_level = 100;
            Dictionary<string, string> conf = getConfig();
            if (!conf.ContainsKey(ConfigStrings.enabled) || !conf[ConfigStrings.enabled].ToLower().Equals("true")) return an_hour_later();
            if (conf.ContainsKey(ConfigStrings.merge_gem_level)) Int32.TryParse(conf[ConfigStrings.merge_gem_level], out merge_gem_level);
            EquipMgr mgr = ServiceFactory.getEquipManager();
            mgr.getStore(_proto, _logger, _user);
            List<Equipment> equipStore = _user._storeEquips;
            // 先合并宝石
            int lowest_gem_level = 0;
            for(int i=0; i<equipStore.Count; i++)
            {
                Equipment equip = equipStore[i];
                if (equip.goodstype == GoodsType.Gem)
                {
                    if (equip.FragmentCount > 1)
                    {
                        if (lowest_gem_level == 0) lowest_gem_level = equip.Level;
                        else if (lowest_gem_level > equip.Level) lowest_gem_level = equip.Level;
                    }
                }
            }
            if (lowest_gem_level > 0 && lowest_gem_level < merge_gem_level)
            {
                int merged_stone_number = mgr.mergeSameLevelGem(_proto, _logger, lowest_gem_level);
                while (merged_stone_number > 1 && lowest_gem_level < merge_gem_level)
                {
                    lowest_gem_level++;
                    merged_stone_number = mgr.mergeSameLevelGem(_proto, _logger, lowest_gem_level);
                }
            }
            // 有兵器先收兵器
            int getEquip_result = 0;
            int currentStore = _user._storeUsedSize;
            int maxStore = _user._storeTotalSize;
            for (int i = 0; i < equipStore.Count; i++)
            {
                Equipment equip = equipStore[i];
                if (equip.RemainTime > 0)
                {
                    if (equip.goodstype == GoodsType.Weapon && currentStore < maxStore)
                    {
                        getEquip_result = mgr.getEquip(_proto, _logger, equip);
                        if (getEquip_result == 2)
                        {
                            currentStore = maxStore;
                            break;
                        }
                        currentStore++;
                    }
                }
            }
            
            // 再看其他, 比如玉佩什么的
            if (getEquip_result == 0)
            {
                for (int i = 0; i < equipStore.Count; i++)
                {
                    Equipment equip = equipStore[i];
                    if (equip.RemainTime > 0)
                    {
                        if (currentStore < maxStore)
                        {
                            getEquip_result = mgr.getEquip(_proto, _logger, equip);
                            if (getEquip_result == 2)
                            {
                                currentStore = maxStore;
                                break;
                            }
                            currentStore++;
                        }
                    }
                }
            }
            _user._storeUsedSize = currentStore;

            return next_halfhour();
        }
    }
}
